package com.cwvs.cat.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.cwvs.cat.MsgInviteActivity;
import com.cwvs.cat.MsgSystemActivity;
import com.cwvs.cat.R;
import com.cwvs.cat.bean.MsgInvite;
import com.cwvs.cat.myview.CustomTextView;

public class MsgSystemAdapter extends BaseAdapter {

	private List<MsgInvite> list_contact;
	private LayoutInflater inflater;
	public MsgSystemAdapter(){
		
	}

	public MsgSystemAdapter(Activity ac, List<MsgInvite> list) {
		// TODO Auto-generated constructor stub
		this.inflater = LayoutInflater.from(ac);
		this.list_contact = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list_contact.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list_contact.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("SimpleDateFormat") @Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder;
		if(convertView==null){
			convertView = inflater.inflate(R.layout.item_list_msg_system, null);
			viewholder = new ViewHolder();
			viewholder.txt_system_message_name=(TextView) convertView.findViewById(R.id.txt_system_message_name);
			viewholder. txt_system_message_time=(TextView) convertView.findViewById(R.id.txt_system_message_time);
			viewholder.content = (TextView) convertView.findViewById(R.id.content);
			viewholder.	agree = (CustomTextView) convertView.findViewById(R.id.agree);
			viewholder.refuse = (CustomTextView) convertView.findViewById(R.id.refuse);
			convertView.setTag(viewholder);
			
		}else{
			viewholder = (ViewHolder) convertView.getTag();
		}
		String time=list_contact.get(position).read_time;  
	    Date date=null;  
	    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    try {
			date=formatter.parse(time);
			SimpleDateFormat formatter2 =new SimpleDateFormat("MM-dd HH:mm:ss");  
		    time=formatter2.format(date);  
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		viewholder.txt_system_message_name.setText(list_contact.get(position).nickname);
		viewholder.content.setText(list_contact.get(position).title);
		viewholder.txt_system_message_time.setText(time);
		
		if (!list_contact.get(position).type.equals("")) {
			System.out.println("type=========="+list_contact.get(position).type);
			switch (Integer.parseInt(list_contact.get(position).type)) {
			case 3:
				break;
			default:
				viewholder.agree.setVisibility(View.VISIBLE);
				viewholder.refuse.setVisibility(View.VISIBLE);
				viewholder.agree.setOnClickListener(new onAgreeListener(list_contact.get(position).msg_id,Integer.parseInt(list_contact.get(position).type)));
				viewholder.refuse.setOnClickListener(new onRefuseListener(list_contact.get(position).msg_id,Integer.parseInt(list_contact.get(position).type)));
				
				break;
			}
		}
		
		
		return convertView;
	}
	public static class onAgreeListener implements View.OnClickListener{
		String msg_id;
		int type;
		public onAgreeListener(String msg_id,int type){
			this.msg_id = msg_id;
			this.type = type;
		}
		
		@Override
		public void onClick(View arg0) {
			MsgSystemActivity.agreeOrNot(msg_id, "1",type);
		}
		
	}
	public static class onRefuseListener implements View.OnClickListener{
		String msg_id;
		int type;
		public onRefuseListener(String msg_id,int type){
			this.msg_id = msg_id;
			this.type = type;
		}
		
		@Override
		public void onClick(View arg0) {
			MsgInviteActivity.agreeOrNot(msg_id, "0",type);
		}
		
	}
	class ViewHolder {
		public TextView txt_system_message_name,content,txt_system_message_time;
		public CustomTextView agree,refuse;
	}

}
